"""
    协程 单线程下的开发，又称微线程
    yield

    第三方工具包greenlet gevent
"""
#from greenlet import greenlet
import gevent
from gevent import monkey
import time

#monkey.patch_all() 将用到 time.sleep代码替换成 gevent.sleep

#gevent.spawn(函数名) : 创建协程对象
#gevent.sleep()
#gevent.join()  等待某个协程对象执行完成
#gevent.joinall()

#def simple_coroutine():
#    print('协程开始')
#    x = yield
#    print(f'接收到的值: {x}')

#coro = simple_coroutine()
#print(coro)
#next(coro)  # 启动协程
#print(coro)
#coro.send(10)  # 向协程发送值

# def sing(obj):
#     print("唱歌")
#     obj.switch()
#     print("唱完歌了")
# def dance(obj):
#     print("跳舞")
#     g1.switch()
#     print("跳完舞了")


# if __name__ == '__main__':
#    g1 = greenlet(lambda :sing(g2))
#    g2 = greenlet(lambda :dance(g1))
#    g1.switch()
#    g2.switch()

def sing():
    print("唱歌")
    gevent.sleep(1)
    print("唱完歌了")
def dance():
    print("跳舞")
    gevent.sleep(1)
    print("跳完舞了")
def dance2():
    print("跳舞2")
    gevent.sleep(1)
    print("跳完舞了2")

if __name__ == '__main__':
    g1 = gevent.spawn(sing)
    g2 = gevent.spawn(dance)
    g3 = gevent.spawn(dance2)

    #g1.join()  # 等待g1对象执行结束

    gevent.joinall([g1, g2, g3])

    #for i in range(28) :
    #    gevent.sleep(1)
    #    print(i)

    print("主程序执行完了")